package org.jeecg.modules.flow.callback.delivery.move;

import cn.hutool.core.util.IdUtil;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.business.transfer.entity.WmsMoveReceipt;
import org.jeecg.modules.business.transfer.service.IWmsMoveReceiptService;
import org.jeecg.modules.business.transfer.vo.WmsMoveReceiptPage;
import org.jeecg.modules.dock.entity.DockTaskRecordMap;
import org.jeecg.modules.dock.service.IDockTaskRecordMapService;
import org.jeecg.modules.flow.callback.delivery.util.CommonDeliveryTaskUtil;
import org.jeecg.modules.flow.sys.common.BaseCustomBeanRun;
import org.jeecg.modules.flow.sys.entity.dto.BaseFlowDto;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 条料调拨因出移库单 保存
 *
 * @author gongwenkai
 * @version 1.0.0
 * @since 2023-11-27
 */
@RequiredArgsConstructor
@Component
public class Y106MoveReceiptSaveBean<T> extends BaseCustomBeanRun<T> {


    private final IWmsMoveReceiptService wmsMoveReceiptService;


    private final CommonDeliveryTaskUtil commonDeliveryTaskUtil;

    private final IDockTaskRecordMapService dockTaskRecordMapService;

    @Override
    public void actureRun() {
        BaseFlowDto baseFlowDto = this.getBaseFlowDto();
        baseFlowDto.translateClass(WmsMoveReceiptPage.class);
        WmsMoveReceiptPage wmsMoveReceiptPage = (WmsMoveReceiptPage)baseFlowDto.getData();
        WmsMoveReceipt wmsMoveReceipt = new WmsMoveReceipt();
        BeanUtils.copyProperties(wmsMoveReceiptPage, wmsMoveReceipt);
        String simpleUUID = IdUtil.simpleUUID();
        commonDeliveryTaskUtil.checkMoveReceiptMaterialWarehouseArea(wmsMoveReceipt.getDocumentType(),wmsMoveReceiptPage.getWmsMoveReceiptMaterialList());
        if (StringUtils.isBlank(wmsMoveReceiptPage.getId())) {
            wmsMoveReceipt.setTaskCode(simpleUUID);
            String receiptId=wmsMoveReceiptService.saveMain(wmsMoveReceipt, wmsMoveReceiptPage.getWmsMoveReceiptMaterialList());
            //将taskCode和receiptId插入dockMap表
            DockTaskRecordMap dockTaskRecordMap=new DockTaskRecordMap();
            dockTaskRecordMap.setTaskId(receiptId);
            dockTaskRecordMap.setMesTaskCode(simpleUUID);
            dockTaskRecordMap.setRemark("移库单创建");
            dockTaskRecordMapService.save(dockTaskRecordMap);
        } else {
            wmsMoveReceiptService.updateMain(wmsMoveReceipt, wmsMoveReceiptPage.getWmsMoveReceiptMaterialList());
        }
        baseFlowDto.setInfoId(wmsMoveReceipt.getId());
        baseFlowDto.setResult(Result.OK());
    }
}
